package com.common.web.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.apache.commons.lang3.StringUtils;
import org.reflections.Reflections;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.common.api.annontation.ServiceLog;
import com.common.api.iface.CodeNameEnumFace;
import com.common.api.model.KeyValuePair;
import com.common.web.annotation.ControllerRequest;
import com.common.web.dto.response.RestResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @author ficus
 *
 */
@ServiceLog
@ControllerRequest(value = "/enums/")
@Api(tags = {"系统工具-枚举类型访问"})
public class EnumsController {
	
	@Value("${reflectionPrefix:}")
	private String reflectionPrefix;
	
	private Map<String,List<KeyValuePair<String,String>>> enumMap=new HashMap<>();
	
	@ApiOperation(value = "获取枚举集合")
	@RequestMapping(value = "getEnums")
	public RestResponse getEnums() {
		return RestResponse.success("执行请求成功").setData(enumMap);
	}
	
	@ApiOperation(value = "获取指定枚举")
	@RequestMapping(value="getSpecEnums")
	public RestResponse getSpecEnums(@RequestBody String enumname) {
		return RestResponse.success("执行请求成功").setData(enumMap.get(enumname));
	}
	
	@PostConstruct
	public void init() {
		String[] prefixs=new String[] {"com.common","com.pmafs.biz"};
		if(StringUtils.isNoneEmpty(reflectionPrefix)) {
			prefixs=reflectionPrefix.split(";");
		}
		for (String string : prefixs) {
			Reflections reflections=new Reflections(string);	
			Set<Class<? extends CodeNameEnumFace>> classes = reflections.getSubTypesOf(CodeNameEnumFace.class);
			List<Class<? extends CodeNameEnumFace>> list=new ArrayList<>(classes);
			Collections.sort(list, new Comparator<Class<? extends CodeNameEnumFace>>() {
				@Override
				public int compare(Class<? extends CodeNameEnumFace> o1, Class<? extends CodeNameEnumFace> o2) {
					return o1.getName().compareTo(o2.getName());
				}
			});
			for (@SuppressWarnings("rawtypes") Class clazz : classes) {
				String className=clazz.getSimpleName();
				if(enumMap.containsKey(className)) {
					className=clazz.getName();
				}
				List<KeyValuePair<String,String>> enumList=new ArrayList<>();
				enumMap.put(className, enumList);
				Object[] objects= clazz.getEnumConstants();
				for (Object object : objects) {
					CodeNameEnumFace codeNameEnumFace=(CodeNameEnumFace) object;
					String code=codeNameEnumFace.getEunmCode();
					String value=codeNameEnumFace.getEnumName();
					KeyValuePair<String, String> pair=new KeyValuePair<String, String>(code, value);
					pair.setExtInfo(codeNameEnumFace.getExtInfo());
					enumList.add(pair);
				}
			}
		}
	}
	
	@PreDestroy
	public void destroy() {
		enumMap.clear();
		enumMap=null;
	}
}
